<?php

/**
 * ensayo actions.
 *
 * @package    hipermedio
 * @subpackage ensayo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ensayoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->ensayos = Doctrine_Core::getTable('Ensayo')
      ->createQuery('a')
      ->execute();
  }
  
  public function executeShow(sfWebRequest $request)
  {
    $this->ensayo = Doctrine_Core::getTable('Ensayo')->find(array($request->getParameter('id')));
    $this->palabras_claves = Doctrine_Core::getTable('Tag')->findByEnsayoId($request->getParameter('id'));
    $this->forward404Unless($this->ensayo);
  }
  
  public function executeAgregar(sfWebRequest $request){
    if ($request->isMethod('post'))
    {
        $nodos = $request->getPostParameter('nodo');
        $ensayo_id = $request->getPostParameter('ensayo');
        
        $ensayo = new Ensayo();
        $ensayo = Doctrine::getTable('Ensayo')->findOneById($ensayo_id['id']);
        
        $this->nodos_id = array();
        
        foreach($nodos as $key => $value){
            
            if($value){
                $tmp = split('_', $value);
                $tag_id = $tmp[1];

                $tag = new Tag();
                $tag = Doctrine::getTable('Tag')->findOneById($tag_id);

                $nivel_padre = $this->verPadre($key);

                $hoja = new Nodo();
                $hoja->setNombre($tag->getTag());
                $hoja->setHipermedioId($ensayo->getHipermedioId());
                $hoja->setNodoPadreId($nivel_padre);
                $hoja->save();

                $this->nodos_id[$key] = $hoja->getId();
            }
        }
        
        $this->redirect('mapa/agregar?hipermedio_id='.$ensayo->getHipermedioId());
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new EnsayoForm();
    
    $this->hipermedio = Doctrine::getTable('Hipermedio')->find(array($request->getParameter('hipermedio_id')));
    $ws = $this->form->getWidgetSchema();
    $ws['hipermedio_id']->setAttribute("value", $this->hipermedio->getId());
    
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    
    $this->form = new EnsayoForm();
    $this->hipermedio = Doctrine::getTable('Hipermedio')->find(array($request->getParameter('hipermedio_id')));
    
    $this->processForm($request, $this->form);
    
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($ensayo = Doctrine_Core::getTable('Ensayo')->find(array($request->getParameter('id'))), sprintf('Object ensayo does not exist (%s).', $request->getParameter('id')));
    $this->form = new EnsayoForm($ensayo);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($ensayo = Doctrine_Core::getTable('Ensayo')->find(array($request->getParameter('id'))), sprintf('Object ensayo does not exist (%s).', $request->getParameter('id')));
    $this->form = new EnsayoForm($ensayo);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($ensayo = Doctrine_Core::getTable('Ensayo')->find(array($request->getParameter('id'))), sprintf('Object ensayo does not exist (%s).', $request->getParameter('id')));
    $ensayo->delete();

    $this->redirect('ensayo/index');
  }

  public function executeMostrarArreglo(sfWebRequest $request){
    
    print_r($request->getPostParameters());
    $palabras = $request->getPostParameter('palabras');
    
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ensayo = $form->save();
      
      // Obtenemos el array de palabras
      $json = json_decode($ensayo->getArreglo(), true);
      $palabras = $json['palabras'];
      
      foreach($palabras as $palabra){
          
          if($palabra['contador'] > 3){
              $tag = new Tag();
              $tag->setEnsayoId($ensayo->getId());
              $tag->setTag($palabra['nombre']);
              $tag->setContador($palabra['contador']);
              $tag->save();
          }
      }
      
      $this->redirect('mapa/edit?hipermedio_id='.$ensayo->getHipermedioId());
    }
  }
  
  protected function verPadre($string){
      
      // Contamos la cantidad de puntos
      $separado = explode('.', $string);
      
      //Retorna el penúltimo
      if(count($separado) > 1){
          $padre = $separado[0];
          
          for($i=1; $i < count($separado) - 1; $i++){
              $padre = $padre.'.'.$separado[$i];
          }
          return $this->nodos_id[$padre];
      }
      else{
          return null;
      }
      
      
      
  }
}
